package com.yunhe.rentcars.pro.controller;

import com.google.code.kaptcha.Constants;

import com.yunhe.rentcars.commons.dto.JSONResponse;
import com.yunhe.rentcars.commons.dto.Menu;
import com.yunhe.rentcars.commons.util.DaoFactory;
import com.yunhe.rentcars.pro.dao.permi.UserDao;
import com.yunhe.rentcars.pro.entity.permi.Permission;
import com.yunhe.rentcars.pro.entity.permi.User;
import com.yunhe.rentcars.pro.service.permi.LoginService;
import com.yunhe.rentcars.pro.service.permi.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ljh201314 on 2017/10/15.
 */
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private UserService userService;
    @Autowired
    private LoginService loginService;

    @RequestMapping
    public String loginUi(){
        return "login";
    }



    @RequestMapping("/check")
    @ResponseBody
    public JSONResponse login(HttpServletRequest req, HttpServletResponse resp, String userName, String password , String imgYzm , String autoLogin) throws SQLException {

        HttpSession session = req.getSession();
        String reg_captcha = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        Date reg_captcha_date = (Date)session.getAttribute(Constants.KAPTCHA_SESSION_DATE);

        int code = JSONResponse.CODE_SUCCESS;
        String message = "登录成功";
        if(null == reg_captcha || null == reg_captcha_date || (new Date().getTime() - reg_captcha_date.getTime())/60000> 5 || !reg_captcha.equalsIgnoreCase(imgYzm)){
            code = JSONResponse.CODE_FAIL;
            message = "验证码输入错误或已过期";
        }else{

             User u  = new User();
             u.setUsername(userName);
             u.setPassword(password);

            User user = userService.find(u);

            if(null == user){
                code = JSONResponse.CODE_FAIL;
                message = "用户名或密码输入错误";
            }else{
                session.setAttribute("user",user);
                session.setAttribute("userName",userName);

               /* List<Menu> menuList = loginService.leftMenu(user.getRoleId());*/

                List<Menu> menuList = loginService.findPermiMenu(user.getRolecode());
                session.setAttribute("menuList",menuList);

                Map<String, Permission> allPermission = loginService.allPermission(user.getRolecode());
                session.setAttribute("allPermission",allPermission);

                loginService.autoLogin(req,resp,autoLogin,userName,password);


            }
        }
        session.removeAttribute(Constants.KAPTCHA_SESSION_KEY);
        session.removeAttribute(Constants.KAPTCHA_SESSION_DATE);
        return  new JSONResponse(code,message);

    }


    @RequestMapping("/findUser")
    @ResponseBody
    public JSONResponse findUser(String userName){

        User u  = new User();
        u.setUsername(userName);

        UserDao userDao = new DaoFactory<UserDao>().getDao(UserDao.class);
        User user = userDao.find(u);
        if(null == user){
            return JSONResponse.fail("用户名不存在");
        }else{
            return JSONResponse.success("用户名输入正确");
        }

    }


    @RequestMapping("/tpmjy")
    @ResponseBody
    public JSONResponse tpmjy(HttpServletRequest req, String imgYzm){
        HttpSession session = req.getSession();
        String reg_captcha = (String)session.getAttribute(Constants.KAPTCHA_SESSION_KEY);
        Date reg_captcha_date = (Date)session.getAttribute(Constants.KAPTCHA_SESSION_DATE);
        long sss = new Date().getTime() - reg_captcha_date.getTime();

        if( (sss/(1000*60)>5)|| !reg_captcha.equalsIgnoreCase(imgYzm)){
           return JSONResponse.fail("验证码输入错误或已过期");
        }else{
            return JSONResponse.success("验证码输入正确");
        }
    }
}
